<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Item_support extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$stat = intval($this->input->get('stat'));
		$now = time();

		if ($stat == 1) {
			$item_ids = $this->d->fields(array('table' => 'items', 'where' => 'status=2 and end>' . $now, 'field' => 'id'));

			if (!$item_ids) {
				$item_ids = 0;
			}

			$where = ' and item_id in (' . $item_ids . ')';
		}
		else if ($stat == 2) {
			$item_ids = $this->d->fields(array('table' => 'items', 'where' => 'status=2 and end<=' . $now, 'field' => 'id'));

			if (!$item_ids) {
				$item_ids = 0;
			}

			$where = ' and item_id in (' . $item_ids . ')';
		}
		else {
			$where = '';
		}

		$d = $this->result_list(array('table' => 'support', 'where' => 'user_id=' . $this->user['id'] . $where, 'order' => 'add_time desc', 'url' => 'item_support/index', 'per_page' => 10));

		foreach ($d['list'] as $k => $v) {
			$d['list'][$k]['coin'] = $this->d->find(array('table' => 'coin', 'where' => 'id=' . $v['coin_id']));
			$item = $this->d->find(array('table' => 'items', 'where' => 'id=' . $v['item_id']));
			$item['db_name'] = $this->d->field(array('table' => 'coin', 'where' => 'id=' . $item['db_id'], 'field' => 'en_name'));

			if (time() < $item['begin']) {
				$item['stat'] = 0;
			}
			else {
				if (($item['begin'] <= time()) && (time() < $item['end'])) {
					if ($item['is_over'] == 1) {
						$item['stat'] = 1;
					}
					else if ($item['target_amount'] <= $item['deal']) {
						$item['stat'] = 2;
					}
					else {
						$item['stat'] = 1;
					}
				}
				else {
					$item['stat'] = 2;
				}
			}

			$d['list'][$k]['item'] = $item;
		}

		$d['stat'] = $stat;
		$this->show(array('header', 'user_left', 'item_support', 'footer'), $d);
	}
}

?>
